﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class RC_WaitingListForPlacement : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IMasNamePrefixBLL prefixBLL = new MasNamePrefixBLL();
        private IMasGenderBLL genderBLL = new MasGenderBLL();
        private IMasNationalitiesBLL natBLL = new MasNationalitiesBLL();
        private IEmployeeTypeBLL emptypeBLL = new EmployeeTypeBLL();

        private PersonalInfo personInfo;
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                bindEmpType();
                BindStatusCombo();
                BindGenderRadio();             
                BindNationalCombo();

                this.ViewState.Add("IsUserControlPostBack", true);

            }

            if (DataItem != null)
            {

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {

                    lblTmpEmpSeq.Text = DataBinder.Eval(DataItem, "tmp_emp_seq").ToString();

                    employeeTypeCombo.SelectedValue = DataBinder.Eval(DataItem, "emp_type_seq").ToString();
                    rdoGender.SelectedValue = DataBinder.Eval(DataItem, "gender_seq").ToString();
                    BindNamePrefixCombo();
                    namePrefixCombo.SelectedValue = DataBinder.Eval(DataItem, "name_prefix_seq").ToString();
                    nationalCombo.SelectedValue = DataBinder.Eval(DataItem, "nat_seq").ToString();
                    statusCombo.SelectedValue = DataBinder.Eval(DataItem, "emp_status_seq").ToString();
                    txtFirstname.Text = DataBinder.Eval(DataItem, "first_name").ToString();
                    txtLastName.Text = DataBinder.Eval(DataItem, "last_name").ToString();
                    txtBirthDate.SetValue(DataBinder.Eval(DataItem, "birth_date", "{0:dd/MM/yyyy}").ToString());
                }
                else
                {
                    employeeTypeCombo.SelectedValue = Session["EMPTYPE"].ToString();
                }
            }
        }
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        private void BindNationalCombo()
        {
            IList<MasNationalitiesInfo> infoList = natBLL.ListNationalities();

            nationalCombo.DataSource = infoList;
            nationalCombo.DataBind();
        }

        private void BindGenderRadio()
        {
            IList<MasGenderInfo> InfoList = genderBLL.ListMasGenders();

            rdoGender.DataSource = InfoList;
            rdoGender.DataBind();
        }

        private void BindNamePrefixCombo()
        {
            Nullable<int> GenderSeq = rdoGender.SelectedValue.Length > 0 ? Int32.Parse(rdoGender.SelectedValue) : (int?)null;
            DataSet ds = prefixBLL.LoadNamePrefixes((int?)null, GenderSeq);
            if (ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                namePrefixCombo.DataSource = dt;
                namePrefixCombo.DataBind();
            }
        }

        private void BindStatusCombo()
        {
            DataTable dt;
            WaitingListForPlacementBLL obj = new WaitingListForPlacementBLL();

            dt = obj.SelectStatusCombo();

            statusCombo.DataSource = dt;
            statusCombo.DataBind();
        }

        private void bindEmpType()
        {
            IList<EmployeeTypeInfo> infoList = emptypeBLL.ListEmployeeTypes();
            employeeTypeCombo.DataSource = infoList;
            employeeTypeCombo.DataBind();
        }

        protected void SetPersonalInfo()
        {
            personInfo = new PersonalInfo();

            personInfo.PersonSeq = lblTmpEmpSeq.Text.Length > 0 ? Int32.Parse(lblTmpEmpSeq.Text) : (int?)null;
            personInfo.GenderSeq = rdoGender.SelectedValue.Length > 0 ? Int32.Parse(rdoGender.SelectedValue) : (int?)null;
            personInfo.NamePrefixSeq = namePrefixCombo.SelectedValue.Length > 0 ? Int32.Parse(namePrefixCombo.SelectedValue) : (int?)null;
            personInfo.FirstName = txtFirstname.Text.Length > 0 ? txtFirstname.Text : "";
            personInfo.National = nationalCombo.SelectedValue.Length > 0 ? Int32.Parse(nationalCombo.SelectedValue) : (int?)null;
            personInfo.LastName = txtLastName.Text.Length > 0 ? txtLastName.Text : "";
            personInfo.BirthDate = txtBirthDate.GetDateValue();

            if (nationalCombo.SelectedValue == "1")
                personInfo.Citizen = txtCitizen.Text.Length > 0 ? txtCitizen.Text : "";
            else
                personInfo.Citizen = txtCitizen2.Text.Length > 0 ? txtCitizen2.Text : "";

            personInfo.PersonalStatus = string.IsNullOrEmpty(statusCombo.SelectedValue) ? (int?)null : Int32.Parse(statusCombo.SelectedValue);
            personInfo.CreatedBy = ssMng.EmpSeqI;
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            SetPersonalInfo();

            int emp_type_seq = int.Parse(employeeTypeCombo.SelectedValue);     
            int ent_type_seq = int.Parse(Session["EntTypeSeq"].ToString());        

            WaitingListForPlacementBLL obj = new WaitingListForPlacementBLL();

            obj.InsertTmpPerson(personInfo,emp_type_seq, ent_type_seq);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int emp_type_seq = int.Parse(employeeTypeCombo.SelectedValue);
            int ent_type_seq = int.Parse(Session["EntTypeSeq"].ToString()); 

            SetPersonalInfo();

            WaitingListForPlacementBLL obj = new WaitingListForPlacementBLL();

            obj.UpdateTmpPerson(personInfo, int.Parse(lblTmpEmpSeq.Text)
                , emp_type_seq, ent_type_seq, ssMng.EmpSeqI);

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }


        protected void txtCitizen2_TextChanged(object sender, EventArgs e)
        {


        }

        protected void nationalCombo_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            bool bNationThai = this.nationalCombo.SelectedValue == "1";

            txtCitizen.Visible = bNationThai;
            txtCitizen2.Visible = !bNationThai;

        }

        protected void txtCitizen_TextChanged(object sender, EventArgs e)
        {

        }

        protected void rdoGender_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindNamePrefixCombo();
        }

    }
}